//
//  OilCardQueryListTableViewController.m
//  JxCarios
//
//  Created by xcq on 15/2/11.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "OilCardQueryListTableViewController.h"
 #import "OilCardSlideDialDetailViewController.h"
#import "OilCardQueryViewController.h"
#import "OilCardTableViewCell.h"
#import "OilCardPayViewController.h"
#import "CLUIBaseNavigationController.h"
static NSString *cellIndentifire = @"OilCard";
static BOOL tableViewIsload = NO;
@interface OilCardQueryListTableViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation OilCardQueryListTableViewController

- (void)viewWillAppear:(BOOL)animated{
    if (_carAround) {
        [self netQuest];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAbout];
    [self netQuest];
}


- (void)initAbout {
    self.title = @"油卡详情";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    [self.listTableView registerNib:[UINib nibWithNibName:@"OilCardTableViewCell" bundle:nil] forCellReuseIdentifier:cellIndentifire];
    self.listTableView.tableFooterView = [UIView new];
}

- (void)netQuest {
    if(!_carAround){
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    
    [_carAround queryOilCardDetail:^(id result) {
        _tableDataSouce = [NSMutableArray arrayWithArray:[result customObjectForkey:@"card"]];
        [self initDataSource];
    } anfFail:^(id result) {
        
    }];
    
}

- (void)initDataSource {
    if (_tableDataSouce.count <= 0) {
        return;
    }
    tableViewIsload = YES;
    if(!self.listTableView.delegate){
        self.listTableView.delegate = self;
        self.listTableView.dataSource = self;
    }
    [self.listTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark tableView DataSource & delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tableDataSouce.count <= 0 ? 0 : _tableDataSouce.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OilCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifire forIndexPath:indexPath];
    cell.info = [_tableDataSouce objectAtIndex:indexPath.row];
    cell.target = self;
    cell.action = @selector(chongzhiAction:);
     return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleNone;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。
    if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
        if (indexPath.row<[self.tableDataSouce count]) {
            [self deleteOilCard:[[self.tableDataSouce objectAtIndex:indexPath.row] customObjectForkey:@"CARD_NUMBER"]];
            [self.tableDataSouce removeObjectAtIndex:indexPath.row];//移除数据源的数据
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的数据
            
        }
    }
}

- (void)deleteOilCard:(NSString *)oilCardNum {
    if (!self.carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    _carAround.oilCard = oilCardNum;
    [_carAround deleteOilCard:^(id result) {
        [self.view makeToast:@"删除成功" duration:1.0f position:TOP_POINT];
    } anfFail:^(id result) {
        
    }];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 72;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OilCardSlideDialDetailViewController *slide = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"oilCardSlide"];
    slide.oilCardNum = [[_tableDataSouce objectAtIndex:indexPath.row] customObjectForkey:@"CARD_NUMBER"];
    [self.navigationController pushViewController:slide animated:YES];
}

- (void)chongzhiAction:(NSDictionary *)info{
    //TODO:跳转充值界面
    //info:_info
    //cell:self
    OilCardPayViewController *oil = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"oilCardPay"];
    oil.oidNum = [[info customObjectForkey:@"info"] customObjectForkey:@"CARD_NUMBER"];
     CLUIBaseNavigationController *nav = [[CLUIBaseNavigationController alloc] initWithRootViewController:oil];
    [self.navigationController pushViewController:oil animated:YES];
    
 }

- (IBAction)bangdingAction:(id)sender {
    OilCardQueryViewController *oil = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"oilQuery"];
    [self.navigationController pushViewController:oil animated:YES];
    
}
@end
